package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.api.IArticleClient;
import com.heima.api.wemedia.WemediaClient;
import com.heima.common.constants.UserConstants;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserRealnameDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.UserRealnameService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * @Description 用户审核
 * @Author bo.li
 * @Date 2023/3/13 11:26
 * @Version 1.0
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements UserRealnameService {

    private final ApUserMapper apUserMapper;

    private final WemediaClient wemediaClient;

    private final IArticleClient articleClient;

    @Override
    public PageResponseResult getList(ApUserRealnameDto dto) {
        dto.checkParam();

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<ApUserRealname>().eq(dto.getStatus() != null,ApUserRealname::getStatus, dto.getStatus());
        Page<ApUserRealname> page = page(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        //用户审核身份证信息脱敏
        page.getRecords().forEach(item -> {
            item.setIdno(item.getIdno().substring(0, 3) + "**********" + item.getIdno().substring(14));
        });
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public ResponseResult authFail(ApUserRealnameDto dto) {
        ApUserRealname userRealname = getById(dto.getId());
        userRealname.setReason(dto.getReason());
        userRealname.setStatus(UserConstants.AUTH_STATUS_FAIL);
        userRealname.setUpdatedTime(LocalDateTime.now());
        updateById(userRealname);
        return ResponseResult.okResult(null);
    }

    @Override
    @Transactional
    public ResponseResult authPass(ApUserRealnameDto dto) {
        //app端审核表状态修改
        ApUserRealname userRealname = getById(dto.getId());
        userRealname.setStatus(UserConstants.AUTH_STATUS_PASS);
        userRealname.setUpdatedTime(LocalDateTime.now());
        updateById(userRealname);
        //app端user表改状态
        Integer userId = userRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        apUser.setFlag(UserConstants.USER_FLAG_MEDIA);
        apUser.setIsCertification(UserConstants.USER_CERTIFID);
        apUserMapper.updateById(apUser);
        //媒体端新增User
        wemediaClient.addWmUser(apUser);

        //article的author表新增
        articleClient.addAuthor(apUser.getName(), userId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}